/*
    直接对所有网络请求结果进行抓包
    tage_API用于过滤
*/

Java.perform(function(){
    let DBo = Java.use("X.DBo");
    let CUE = Java.use("X.CUE");
    let tage_API = "v2/oec/live/bag/assemble"

    function readInputStream(inp) {
        // 确保是 Java 对象
        var InputStream = Java.use('java.io.InputStream');
        inp = Java.cast(inp, InputStream);

        var baos = Java.use('java.io.ByteArrayOutputStream').$new();

        // 创建 Java 的 byte[] 缓冲区（1024）
        var buf = Java.array('byte', Array(1024).fill(0));

        var len;
        while ((len = inp.read(buf)) !== -1) {
            // 注意：write(byte[], offset, len)
            baos.write(buf, 0, len);
        }

        // 得到 byte[] 并转换为 String（UTF-8）
        var bytes = baos.toByteArray();
        var JString = Java.use('java.lang.String');
        var s = JString.$new(bytes, 'UTF-8');

        return { bytes: bytes, text: s };
    }

    DBo.LIZLLL.implementation = function(responseBody,requestConst){
        //console.log("DBo.LIZLLL被调用")
        const result = this.LIZLLL(responseBody,requestConst);
        try {
            var obj = requestConst.getClass();
            let LIZfield = obj.getDeclaredField("LIZ");
            let url = LIZfield.get(requestConst);
            let LJfield =obj.getDeclaredField("LJ");
            
            var Gson = Java.use('com.google.gson.Gson');
            var gson = Gson.$new();
            if(url.toString().indexOf(tage_API) !== -1){

            console.log("\n\n\n" + "===================  找到DBo咯  ===================");
            console.log("url："+ url);


            
            try {
                let responseBodyString = responseBody.toString();
                console.log("bodyString:"+ responseBodyString);
                //logToFile(responseBodyString)
            } catch (e) {
                console.log("body:"+ responseBody)
                //logToFile(responseBody)
                console.log("转换string出错："+ e);
            }
            
            console.log("===========================输出完一条DBo咯====================================\n\n\n");
            }
            
        } catch (e) {
                console.log("获取类失败："+e);
            }
        return result;
    }


    // CUE.$init.overload('java.lang.String', 'int', 'java.lang.String', 'java.util.List', 'com.bytedance.retrofit2.mime.TypedInput')
    // .implementation = function(url,code,status,headersList,typedInput){
        
    //     result = this.$init(url,code,status,headersList,typedInput);
    //     if(url.toString().indexOf(tage_API) !== -1){
    //         console.log("CUE.$init被调用")
    //         console.log("\n\n\n" + "===================  找到CUE咯  ===================");
    //         try {
    //             console.log("url："+ url);
    //             console.log("code："+ code);
    //             console.log("status："+ status);
    //             console.log("headersList："+ headersList);
    //             console.log("typedInput："+ typedInput);
    //             console.log("数据流类型："+typedInput.in().getClass().getName())
    //             //console.log("数据流转换："+readInputStream(typedInput))
    //         } catch (e) {
    //             console.log("转换数据流出错："+e)
    //         }
    //         console.log("===========================输出完一条CUE咯====================================\n\n\n");
    //     }
    //     console.log("CUE.$init返回："+result);
    //     return result;
    // }

    // CUE["$init"].implementation = function () {
    //     console.log('$init is called');
    //     let ret = this.$new();
    //     console.log('$init ret value is ' + ret);
    //     return ret;
    // };
})